/*
 * @Author: your name
 * @Date: 2020-09-22 17:23:28
 * @LastEditTime: 2021-01-26 09:27:39
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \longh_admin\src\store\modules\permission.js
 */
import { asyncRouterMap, constantRouterMap } from '@/config/router.config'

/**
 * 过滤账户是否拥有某一个权限，并将菜单从加载列表移除
 *
 * @param permission
 * @param route
 * @returns {boolean}
 */
function hasPermission(permission, route) {
  if (route.meta && route.meta.permission) {
    let flag = false
    for (let i = 0, len = permission.length; i < len; i++) {
      flag = route.meta.permission.includes(permission[i])
      if (flag) {
        return true
      }
    }
    return false
  }
  return true
}

/**
 * 单账户多角色时，使用该方法可过滤角色不存在的菜单
 *
 * @param roles
 * @param route
 * @returns {*}
 */
// eslint-disable-next-line
function hasRole(roles, route) {
  if (route.meta && route.meta.roles) {
    return route.meta.roles.includes(roles.id)
  } else {
    return true
  }
}

function filterAsyncRouter(routerMap, roles) {

  var ARR = [];

  for (let index = 0; index < roles.length; index++) {
    const element = roles[index];
    for (let index1 = 0; index1 < routerMap[0].children.length; index1++) {
      const element2 = routerMap[0].children[index1];
      // console.log(JSON.stringify(element2))

      console.log(element.funId == element2.funId)
      if (element.funId == element2.funId) {
        ARR.push(element2)
        var ARRChild = [];
        if (element.children) {
          for (let index3 = 0; index3 < element.children.length; index3++) {
            for (let index4 = 0; index4 < element2.children.length; index4++) {
              if (element.children[index3].funId == element2.children[index4].funId) {
                ARRChild.push(element2.children[index4])
              }
            }
          }
          element2.children = ARRChild
        } else { }
      }

    }
  }
  let error = {
    path: '*',
    redirect: '/404',
    hidden: true
  }
  let newARR = ARR
  let _newArray = []
  let NewrouterMap = asyncRouterMap
  console.log('newARR')
  NewrouterMap[0].children = newARR
  console.log('NewrouterMap')
  console.log(NewrouterMap)
  console.log('NewrouterMap')
  return NewrouterMap


}

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({ commit }, data) {
     let _type = process.env.NODE_ENV == 'production'  //是否线上环境
      // if(_type){
        return new Promise(resolve => {
          // 注释异步
          // const { roles } = data
          // const accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
          // // console.log('就')
          // console.log(accessedRouters)
          // commit('SET_ROUTERS', accessedRouters)
          // resolve(accessedRouters)
          // 本地
          resolve(asyncRouterMap)
        })
      // }else{
      //   resolve(asyncRouterMap)
      // }
    }

    // GenerateRoutes({
    //   commit
    // }, roles) {
    //   return new Promise(resolve => {
    //     // let accessedRouters
    //     // if (roles.includes('admin')) {
    //     //   accessedRoutes = asyncRoutes || []
    //     // } else {
    //     // accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
    //     // }
    //     // commit('SET_ROUTES', accessedRouters)

    //     // var ARR = [];
    //     // console.log('roles')
    //     // let _roles = {roles}
    //     // console.log(_roles.roles)
    //     // console.log('roles')
    //     // for (let index = 0; index < roles.length; index++) {
    //     //   const element = roles[index];
    //     //   for (let index1 = 0; index1 < asyncRouterMap[0].children.length; index1++) {
    //     //     const element2 = asyncRouterMap[0].children[index1];
    //     //     console.log(JSON.stringify(element2))

    //     //     console.log(element.funId == element2.funId)
    //     //     if (element.funId == element2.funId) {
    //     //       ARR.push(element2)
    //     //       var ARRChild = [];
    //     //       if (element.children) {
    //     //         for (let index3 = 0; index3 < element.children.length; index3++) {
    //     //           for (let index4 = 0; index4 < element2.children.length; index4++) {
    //     //             if (element.children[index3].funId == element2.children[index4].funId) {
    //     //               ARRChild.push(element2.children[index4])
    //     //             }
    //     //           }
    //     //         }
    //     //         element2.children = ARRChild
    //     //       } else { }
    //     //     }

    //     //   }
    //     // }
    //     // let error = {
    //     //   path: '*',
    //     //   redirect: '/404',
    //     //   hidden: true
    //     // }
    //     // let newARR = ARR
    //     // let _newArray = []
    //     // let NewrouterMap = JSON.parse(JSON.stringify(asyncRouterMap))
    //     // NewrouterMap[0].children = newARR
    //     // console.log('newARR')
    //     // console.log(newARR)
    //     // console.log('NewrouterMap')
    //     // console.log(NewrouterMap)
    //     // return NewrouterMap
    //     resolve(NewrouterMap)
    //   })
    // }
  }
}

export default permission
